package com.kasax.android.arch.webview;

import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ProgressBar;

import com.kasax.android.arch.webview.callback.WebChromeClientCallback;
import com.kasax.android.arch.webview.callback.WebViewClientCallback;
import com.kasax.android.arch.webview.client.InternalWebChromeClient;
import com.kasax.android.arch.webview.client.InternalWebviewClient;


public class WebViewUtils {


    /**
     * 配置webview
     *
     * @param webView https://www.51cto.com/article/786352.html
     * @return
     */
    public static WebSettings configWebView(WebView webView) {
        return configWebView(webView, null, null, null);
    }

    /**
     * 配置webview
     *
     * @param webView https://www.51cto.com/article/786352.html
     * @return
     */
    public static WebSettings configWebView(WebView webView, ProgressBar pb, View holderView, View errorView) {
        // 设置硬件加速
        webView.setLayerType(WebView.LAYER_TYPE_HARDWARE, null);

        // 获取WebView的WebSettings对象
        WebSettings webSettings = webView.getSettings();

        webSettings.setAllowFileAccess(true);
        webSettings.setAllowContentAccess(true);

        // 启用缓存
        webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);

//        // 启用应用缓存
//        webSettings.setAppCacheEnabled(true);
//        // 设置缓存大小
//        long cacheSize = 8 * 1024 * 1024; // 8 MB
//        webSettings.setAppCacheMaxSize(cacheSize);

        // 启用JavaScript支持
        //支持js交互，动画等操作会造成cpu、电量消耗可在activity、 fragment的onResume、onStop进行开关控制
        webSettings.setJavaScriptEnabled(true);

        // 启用DOM存储API支持
        webSettings.setDomStorageEnabled(true);

        // 启用数据库存储API支持
        webSettings.setDatabaseEnabled(true);

        // 设置Web视口的宽度适应屏幕
        // 自适应屏幕->自动将图片调整到自适应webView 大小
        webSettings.setUseWideViewPort(true);
        // //自适应屏幕->缩放到屏幕大小
        webSettings.setLoadWithOverviewMode(true);

        //先加载文字，再加载图片
        webSettings.setBlockNetworkImage(true);

        //缩放
        //支持缩放，默认为true。
        webSettings.setSupportZoom(true);
        //设置是否展示内置的缩放控件，默认为false
        webSettings.setBuiltInZoomControls(true);
        //显示原生的缩放控件
        webSettings.setDisplayZoomControls(true);

        webView.setWebChromeClient(new InternalWebChromeClient(new WebChromeClientCallback() {
            @Override
            public void onProgressChanged(int newProgress) {
                if (pb != null) {
                    pb.setProgress(newProgress);
                }
            }
        }));
        webView.setWebViewClient(new InternalWebviewClient(new WebViewClientCallback() {
            @Override
            public void onLoadStart() {
                if (holderView != null) {
                    holderView.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onLoadComplete(boolean hasError) {
                if (holderView != null) {
                    holderView.setVisibility(View.GONE);
                }
                if (errorView != null) {
                    errorView.setVisibility(hasError ? View.VISIBLE : View.GONE);
                }
                webView.setVisibility(hasError ? View.GONE : View.VISIBLE);
            }
        }));
        return webSettings;
    }


}
